//
//  IndexVC.m
//  YunFu
//
//  Created by Bager on 2018/8/29.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "IndexVC.h"

#import "CyclePagerView.h"
#import "IndexCollectionView.h"
#import "LMJScrollTextView2.h"
#import "IndexServiceView.h"
#import "IndexShopNavView.h"
#import "IndexMarketOnlineView.h"
#import "IndexOtherView.h"

#import "HistorySearchVC.h"
#import "SAIndexTabBarVC.h"

@interface IndexVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) IndexCollectionView *indexCV;
@property (nonatomic, strong) UIView *rollTextView;
@property (nonatomic, strong) LMJScrollTextView2 *scrollTextView;
@property (nonatomic, strong) UIView *marketView;
@property (nonatomic, strong) IndexServiceView *serviceView;
@property (nonatomic, strong) IndexShopNavView *shopNavView;
@property (nonatomic, strong) IndexMarketOnlineView *marketOnlineView;
@property (nonatomic, strong) IndexOtherView *welfareView;
@property (nonatomic, strong) IndexOtherView *supplyView;
@property (nonatomic, strong) IndexOtherView *financialView;

@end

@implementation IndexVC

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    
    [self createHeader];
    
    [self createTable];
}

- (void)createHeader
{
    UIView *headerBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenHeight, kPublicNavHeight)];
    headerBg.backgroundColor = kPublicColorMain;
    [self.view addSubview:headerBg];
    
    [self creatSearchNaviBar];
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setFrame:CGRectMake(0, 20, 34, 44)];
    [btn setTitle:@"珲春" forState:UIControlStateNormal];
    btn.titleLabel.numberOfLines = 2;
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    btn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(testClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn.frame) - 3, 20, 10, 44)];
    [img setImage:[UIImage imageNamed:@"yf1-Arrow3"]];
    img.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:img];
}

- (void)testClick
{
    SAIndexTabBarVC *sa = [SAIndexTabBarVC new];
    sa.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:sa animated:YES];
}

- (void)creatSearchNaviBar
{
    LLSearchNaviBarView *searchNaviBarView = [LLSearchNaviBarView new];
    searchNaviBarView.searbarPlaceHolder = @"请输入搜索关键词";
    
    @LLWeakObj(self);
    [searchNaviBarView showbackBtnWith:[UIImage imageNamed:@"navi_back_w"] onClick:^(UIButton *btn) {
        @LLStrongObj(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [searchNaviBarView showRightOneBtnWith:[UIImage imageNamed:@"sandian"] onClick:^(UIButton *btn) {
        
    }];
    
    [searchNaviBarView setSearchBarBeignOnClickBlock:^{
        @LLStrongObj(self);
        
        HistorySearchVC *searShopVC = [HistorySearchVC new];
        
        
        @LLWeakObj(searShopVC);
        //(1)点击分类 (2)用户点击键盘"搜索"按钮  (3)点击历史搜索记录
        [searShopVC beginSearch:^(NaviBarSearchType searchType,NBSSearchShopCategoryViewCellP *categorytagP,UILabel *historyTagLabel,LLSearchBar *searchBar) {
            @LLStrongObj(searShopVC);
            
            NSLog(@"搜索结果：historyTagLabel:%@--->searchBar:%@--->categotyTitle:%@--->%@",historyTagLabel.text,searchBar.text,categorytagP.categotyTitle,categorytagP.categotyID);
            
            searShopVC.searchBarText = @"你选择的搜索内容显示到这里";
        }];
        
        //执行即时搜索匹配
        NSArray *tempArray = @[@"Java", @"Python"];
        
        [searShopVC searchbarDidChange:^(NaviBarSearchType searchType, LLSearchBar *searchBar, NSString *searchText) {
            @LLStrongObj(searShopVC);
            
            NSLog(@"搜索文字++++++%@", searchText);
            
            //FIXME:这里模拟网络请求数据!!!
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                searShopVC.resultListArray = tempArray;
            });
        }];
        
        [searShopVC resultListViewDidSelectedIndex:^(UITableView *tableView, NSInteger index) {
            NSLog(@"搜索结果++++++%@", tempArray[index]);
        }];
        
        [self presentViewController:searShopVC animated:nil completion:nil];
    }];
    
    [self.view addSubview:searchNaviBarView];
}


- (void)createTable
{
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, kPublicNavHeight, kScreenWidth, kScreenHeight - kPublicNavHeight - 49)];
    table.delegate = self;
    table.dataSource = self;
    table.showsVerticalScrollIndicator = NO;
    table.backgroundColor = HexColor(0xf4f5f6);
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
    UIView *noLine = [[UIView alloc] initWithFrame:CGRectZero];
    table.tableFooterView = noLine;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 180 + kPublicPadding;
    }
    else if (indexPath.row == 1)
    {
        return ((kScreenWidth - kPublicMargin * 2) / 5.0 + 20) * 2 + 10;
    }
    else if (indexPath.row == 2)
    {
        return 50;
    }
    else if (indexPath.row == 3)
    {
        return 365;
    }
    else if (indexPath.row == 4)
    {
        return 70 * 2 + 45;
    }
    else if (indexPath.row == 5)
    {
        return 80 * 2 + 65;
    }
    else if (indexPath.row == 6)
    {
        return 80 * 2 + 45;
    }

    return 185;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = HexColor(0xf4f5f6);

    if (indexPath.row == 0)
    {
        CyclePagerView *page = [[CyclePagerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180) imgArray:@[@"",@"",@""]];
        [cell addSubview:page];
        
        DefineWeakSelf;
        page.callBack = ^(id result) {
            
        };
    }
    else if (indexPath.row == 1)
    {
        [cell addSubview:self.indexCV];
    }
    else if (indexPath.row == 2)
    {
        [cell addSubview:self.rollTextView];
    }
    else if (indexPath.row == 3)
    {
        [cell addSubview:self.marketView];
    }
    else if (indexPath.row == 4)
    {
        [cell addSubview:self.serviceView];
    }
    else if (indexPath.row == 5)
    {
        [cell addSubview:self.shopNavView];
    }
    else if (indexPath.row == 6)
    {
        [cell addSubview:self.marketOnlineView];
    }
    else if (indexPath.row == 7)
    {
        [cell addSubview:self.welfareView];
    }
    else if (indexPath.row == 8)
    {
        [cell addSubview:self.supplyView];
    }
    else if (indexPath.row == 9)
    {
        [cell addSubview:self.financialView];
    }
    
    return cell;
}

- (IndexCollectionView *)indexCV
{
    if (!_indexCV)
    {
        _indexCV = [[IndexCollectionView alloc] init];
    }
    return _indexCV;
}

- (UIView *)rollTextView
{
    if (!_rollTextView)
    {
        _rollTextView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _rollTextView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *imgLeft = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
        [imgLeft setImage:[UIImage imageNamed:@"yf1-yftt"]];
        [imgLeft setContentMode:UIViewContentModeCenter];
        [_rollTextView addSubview:imgLeft];
        
        UIImageView *imgRight = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 80, 0, 80, 50)];
        [imgRight setImage:[UIImage imageNamed:@"yf1-tt1"]];
        [imgRight setContentMode:UIViewContentModeScaleAspectFit];
        [_rollTextView addSubview:imgRight];
        
        [_rollTextView addSubview:self.scrollTextView];
    }
    
    return _rollTextView;
}

- (LMJScrollTextView2 *)scrollTextView
{
    if (!_scrollTextView)
    {
        _scrollTextView = [[LMJScrollTextView2 alloc] initWithFrame:CGRectMake(65, 0, kScreenWidth - 65 - 80, 50)];
        _scrollTextView.textStayTime        = 1;
        _scrollTextView.scrollAnimationTime = 1;
        _scrollTextView.textColor           = [UIColor grayColor];
        _scrollTextView.textFont            = [UIFont systemFontOfSize:12];
        _scrollTextView.layer.cornerRadius  = 3;
        [_scrollTextView startScrollBottomToTopWithNoSpace];
        _scrollTextView.textDataArr = @[@"为什么客人点完餐后，立即收走菜单？",@"2018社保新消息，出现这几种情况将无法领取养老金！"];
    }
    return _scrollTextView;
}

- (UIView *)marketView
{
    if (!_marketView)
    {
        _marketView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 360)];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(80, 24, kScreenWidth - 80 * 2, 1)];
        line.backgroundColor = HexColor(0x0c97ec);
        [_marketView addSubview:line];
        
        UILabel *lineText = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2.0 - 65, 0, 130, 50)];
        lineText.text = @"大型商业线上专区";
        lineText.font = [UIFont systemFontOfSize:14];
        lineText.textAlignment = NSTextAlignmentCenter;
        lineText.backgroundColor = HexColor(0xf4f5f6);
        lineText.textColor = HexColor(0x0c97ec);
        [_marketView addSubview:lineText];
        
        UIImageView *imgUp = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, kScreenWidth, 150)];
        [imgUp setImage:[UIImage imageNamed:@"yf1-sc1"]];
        [imgUp setContentMode:UIViewContentModeScaleAspectFill];
        [_marketView addSubview:imgUp];
        
        UILabel *labelUp = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(imgUp.frame) + 10, 80, 30)];
        labelUp.textAlignment = NSTextAlignmentCenter;
        labelUp.text = @"欧亚";
        labelUp.font = [UIFont systemFontOfSize:14];
        labelUp.backgroundColor = HexColor(0xee416f);
        labelUp.adjustsFontSizeToFitWidth = YES;
        labelUp.textColor = [UIColor whiteColor];
        [_marketView addSubview:labelUp];
        
        UIBezierPath *maskPaths = [UIBezierPath bezierPathWithRoundedRect:labelUp.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayers = [[CAShapeLayer alloc] init];
        maskLayers.frame = labelUp.bounds;
        maskLayers.path = maskPaths.CGPath;
        labelUp.layer.mask  = maskLayers;
        
        UIImageView *imgDown = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imgUp.frame) + 10, kScreenWidth, 150)];
        [imgDown setImage:[UIImage imageNamed:@"yf1-sc2"]];
        [imgDown setContentMode:UIViewContentModeScaleAspectFill];
        [_marketView addSubview:imgDown];
        
        UILabel *labelDown = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(imgDown.frame) + 10, 80, 30)];
        labelDown.textAlignment = NSTextAlignmentCenter;
        labelDown.text = @"现代百货";
        labelDown.font = [UIFont systemFontOfSize:14];
        labelDown.backgroundColor = HexColor(0x0c97ec);
        labelDown.adjustsFontSizeToFitWidth = YES;
        labelDown.textColor = [UIColor whiteColor];
        [_marketView addSubview:labelDown];

        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:labelDown.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = labelDown.bounds;
        maskLayer.path = maskPath.CGPath;
        labelDown.layer.mask  = maskLayer;
    }
    return _marketView;
}

- (IndexServiceView *)serviceView
{
    if (!_serviceView)
    {
        _serviceView = [[IndexServiceView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70 * 2 + 40)];
    }
    return _serviceView;
}

- (IndexShopNavView *)shopNavView
{
    if (!_shopNavView)
    {
        _shopNavView = [[IndexShopNavView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 80 * 2 + 60)];
    }
    return _shopNavView;
}

- (IndexMarketOnlineView *)marketOnlineView
{
    if (!_marketOnlineView)
    {
        _marketOnlineView = [[IndexMarketOnlineView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 80 * 2 + 40)];
    }
    return _marketOnlineView;
}

- (IndexOtherView *)welfareView
{
    if (!_welfareView)
    {
        _welfareView = [[IndexOtherView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 160 + 20)];
        _welfareView.titleStr = @"公益服务";
        _welfareView.imgArray = @[@"yf1-cl3",@"yf1-cl4",@"yf1-fldh9",@"yf1-fldh10",@"yf1-fldh11",@"yf1-fldh12"];
        _welfareView.nameArray = @[@"在线捐赠",@"在线求助",@"捐赠活动",@"志愿者",@"爱心商城",@"发起公益"];
        _welfareView.colorArray = @[HexColor(0xfaa86c)];
        [_welfareView setViews];
    }
    return _welfareView;
}

- (IndexOtherView *)supplyView
{
    if (!_supplyView)
    {
        _supplyView = [[IndexOtherView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 160 + 20)];
        _supplyView.titleStr = @"供应链";
        _supplyView.imgArray = @[@"yf1-cl5",@"yf1-cl6",@"yf1-fldh13",@"yf1-fldh14",@"yf1-fldh15",@"yf1-fldh16"];
        _supplyView.nameArray = @[@"需求发布",@"咨询顾问",@"软件开发",@"商业供应链",@"农业供应链",@"服务业供应链"];
        _supplyView.colorArray = @[HexColor(0xc17cf6)];
        [_supplyView setViews];
    }
    return _supplyView;
}

- (IndexOtherView *)financialView
{
    if (!_financialView)
    {
        _financialView = [[IndexOtherView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 160 + 20)];
        _financialView.titleStr = @"在线金融";
        _financialView.imgArray = @[@"yf1-cl7",@"yf1-cl8",@"yf1-fldh17",@"yf1-fldh18",@"yf1-fldh19",@"yf1-fldh20"];
        _financialView.nameArray = @[@"在线理财",@"保险服务",@"车险",@"财险",@"意外",@"人寿"];
        _financialView.colorArray = @[HexColor(0xfede4f)];
        [_financialView setViews];
    }
    return _financialView;
}





@end
